package com.lanqiao.attendance.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lanqiao.attendance.mapper.AdminMapper;
import com.lanqiao.attendance.mapper.UserMapper;
import com.lanqiao.attendance.pojo.Administrator;
import com.lanqiao.attendance.service.AdminService;
import com.lanqiao.attendance.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper,Administrator> implements AdminService {
    @Autowired(required = false)
    AdminMapper adminMapper;

    /**
     * 注册
     * @return
     */
    @Override
    public int register(Administrator administrator) {
        int register = adminMapper.register(administrator);
        return register;
    }

    /**
     * 根据用户名查找出该用户的信息
     * @param admUsername
     * @return
     */
    @Override
    public Administrator selectOne(String admUsername) {
        Administrator administrator = adminMapper.selectOne(admUsername);
        return administrator;
    }

    /**
     * 新增管理员
     * @param administrator
     * @return
     */
    @Override
    public boolean addAdmin(Administrator administrator) {
        boolean addAdmin = adminMapper.addAdmin(administrator);
        return addAdmin;
    }

    /**
     * 删除管理员
     * @param id
     * @return
     */
    @Override
    public boolean deleteAdmin(long id) {
        boolean deleteAdmin = adminMapper.deleteAdmin(id);
        return deleteAdmin;
    }

    /**
     * 更新管理员
     * @param administrator
     * @return
     */
    @Override
    public int updateAdmin(Administrator administrator) {
        int row = adminMapper.updateAdmin(administrator);
        return row;
    }

    /**
     * 查询 所有
     * @return
     */
    @Override
    public PageInfo<Administrator> selectAll(Integer pageNum,Integer pageSize) {
        if(pageNum == null){
            pageNum =1 ;
        }
        PageHelper.startPage(pageNum,pageSize);
        List<Administrator> administrators = adminMapper.selectAll();
        PageInfo pageInfo = new PageInfo<Administrator>(administrators);

        return pageInfo;
    }

    /**
     * 模糊查询用户名
     * @param pageNum
     * @param pageSize
     * @param admUsername
     * @return
     */
    @Override
    public PageInfo<Administrator> selectByLikeName(Integer pageNum, Integer pageSize, String admUsername) {
        if(pageNum == null){
            pageNum =1 ;
        }

        PageHelper.startPage(pageNum,pageSize);
        List<Administrator> administrators = adminMapper.selectByLikeName(admUsername);
        PageInfo pageInfo = new PageInfo<Administrator>(administrators);

        return pageInfo;
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public Administrator selectById(long id) {
        Administrator administrator = adminMapper.selectById(id);
        return administrator;
    }

    @Override
    public Result queryByAccount(String account) {
        Administrator administrator = adminMapper.queryByAccount(account);
       if(administrator == null){
           return Result.fail(444,"为找到");
       }
       return Result.success("okk",administrator);
    }
}
